package br.com.guarana.util
{
	import mx.controls.ComboBox;
	import mx.controls.DateField;
	import mx.controls.TextInput;
	import mx.events.ValidationResultEvent;
	import mx.validators.DateValidator;
	import mx.validators.NumberValidator;
	import mx.validators.StringValidator;
	
	public class Validacao
	{
		public function Validacao()
		{
		}
		
		public static function validaCampos(...campo):Boolean{
			var res:Boolean = true;
			var msg:String = 'Este campo é obrigatório!';
			for (var i:uint = 0; i < campo.length; i++)	{
				limpaCamposInvalidos(campo[i]);
				if (campo[i] is TextInput){
					var str_v:StringValidator = new StringValidator();
					if (str_v.validate(campo[i].text).type != ValidationResultEvent.VALID){
						res = false;
						campo[i].errorString = msg;
					}
				}
				else if (campo[i] is ComboBox)
				{
					var num_v:NumberValidator = new NumberValidator();
					num_v.minValue = 1;
					if (num_v.validate(campo[i].selectedIndex).type != ValidationResultEvent.VALID){
						res = false;
						campo[i].errorString = msg;
					}
				}
				else if (campo[i] is DateField)
				{
					if (campo[i].selectedDate == null){
						res = false;
						campo[i].errorString = msg;
					} else {
						var date_v:DateValidator = new DateValidator();
						date_v.inputFormat = "DD/MM/YYYY";
						if (date_v.validate(campo[i].text).type != ValidationResultEvent.VALID){
							res = false;
							campo[i].errorString = 'Data Inválida!';
						}
					}
				}
			}
			return res;
		}
		
		public static function limpaCamposInvalidos(...campo):void{
			for (var i:uint = 0; i < campo.length; i++)
				campo[i].errorString = "";
		}
		
	}
}